//	ID : pallab81
// 	PROG : 10626
// 	LANG : C++
// 	2011-09-09-08.52.16 Friday
//
// 	"I have not failed, I have just found 10000 ways that won't work.
//


#include <iostream>
#include <algorithm>
#include <vector>
#include <sstream>
#include <fstream>
#include <string>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <functional>
#include <bitset>

#include <ctime>
#include <cassert>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <climits>
#include <cstring>
#include <cstdlib>

using namespace std;

#define VI vector< int >
#define CI( x ) scanf("%d",&x)
#define CL( x ) scanf("%lld",&x)
#define CD( x ) scanf("%lf",&x )
#define foR(i,st,ed) for(int i = st ; i < ed ; ++i )
#define foE(i,st,ed) for(int i = st ; i <= ed ; ++i )
#define fo(i,ed) for(int i = 0 ; i < ed ; ++i )
#define foit(i, x) for (typeof((x).begin()) i = (x).begin(); i != (x).end(); i++)
#define bip system("pause")
#define Int long long
#define pb push_back
#define SZ(X) (int)(X).size()
#define LN(X) (int)(X).length()
#define mk make_pair
#define f first
#define s second
#define SET( ARRAY , VALUE ) memset( ARRAY , VALUE , sizeof(ARRAY) )

inline void wait( double seconds ){
    double endtime = clock()+( seconds* CLOCKS_PER_SEC );
    while( clock() < endtime ){
        ;
    }
}
int dp[151][510][210];
inline void payBack(int sum, int &a1,int &a5, int &a10){
    a1=a5=a10=0;
    if(sum>=10){
        a10 = sum/10;
        sum%=10;
    }
    if(sum>=5){
        a5 = sum/5;
        sum%=5;
    }
    a1=sum;
return ;
}
int go(int C,int sum,int a1,int a5, int a10){
    if(C==0){
        return 0;
    }
    if(sum==8){
        return go(C-1,0,a1,a5,a10);
    }
    int&best = dp[C][a1][a5];
    if( best!=-1 ){
        return best;
    }
    best= (int)1e6;
    if(a1>0){
        if(sum+1>8){
            int a,b,c;
            payBack(sum+1-8,a,b,c);
            int way1 = 1+go(C,8,a1-1+a,a5+b,a10+c);
            best = min(best,way1);
        }
        else{
            int way1 = 1+go(C,sum+1,a1-1,a5,a10);
            best = min(best,way1);
        }
    }
    if(a5>0){
        if( sum+5>8){
            int a,b,c;
            payBack(sum+5-8,a,b,c);
            int way2 = 1+go(C,8,a1+a,a5-1+b,a10+c);
            best = min(best,way2);
        }
        else{
            int way2 = 1+go(C,sum+5,a1,a5-1,a10);
            best = min(best,way2);
        }
    }
    if(a10>0){
        if(sum+10>8){
            int a,b,c;
            payBack(sum+10-8,a,b,c);
            int way3 = 1+go(C,8,a1+a,a5+b,a10-1+c);
            best = min(best,way3);
        }
        else{
            int way3 = 1+go(C,sum+10,a1,a5,a10-1);
            best = min(best,way3);
        }
    }

return best;
}
int CC,n1,n5,n10;
inline void Read(){
    CI(CC);
    CI(n1);
    CI(n5);
    CI(n10);
}
inline void Proc(){
    SET(dp,-1);
    int var=go(CC,0,n1,n5,n10);
    cout<<var<<"\n";
}
int main(){
    #ifndef ONLINE_JUDGE
        freopen("in.txt","rt",stdin);
    #endif
    //cout<<sizeof(dp)/1024/1024<<"\n";
    int cases=1;
    for( CI(cases) ; cases ; --cases ){
        Read();
        Proc();
    }

return 0;
}
